package com.lbs.jiepiaisi.base;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lbs.jiepiaisi.R;
import com.lbs.jiepiaisi.utils.LoggerUtil;

import butterknife.ButterKnife;

/**
 *
 * 时间：2017/6/22 09:50
 * 作用：Activity的扩展类
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected ViewGroup mainBody; // 主体显示
    protected boolean isTemplate = false; // 是否使用模板
    protected boolean isShowBackBtn = true; // 是否显示返回箭头
    private View titleBar; // 标题栏
    protected TextView titleView; // 标题
    protected ImageButton templateButtonLeft; // 模板中左则Button,不使用模板时，不会显示;
    protected ImageButton templateButtonRight; // 模板中右则Button,不使用模板时，不会显示;
    protected TextView templateTextViewRight; // 模板右侧TextView
    private View reLoadView; // 重新加载的布局

    private LinearLayout backLin;

    protected int page = 1;
    protected int size = 10;
    /**
     * 打印TAG，类名
     */
    private String TAG = getClass().getSimpleName();
    /**
     * 上下文对象，等同于this
     */
    protected Activity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.template);
//        ButterKnife.bind(this);
        mainBody = (ViewGroup) findViewById(R.id.view_mainBody);
        reLoadView = findViewById(R.id.reLoadView);
        reLoadView.setVisibility(View.GONE);
        titleBar = findViewById(R.id.titleBar);
        templateButtonLeft = (ImageButton) findViewById(R.id.title_but_left);
        templateButtonRight = (ImageButton) findViewById(R.id.title_but_right);
        templateTextViewRight = (TextView) findViewById(R.id.title_tv_right);

        templateButtonLeft.setOnClickListener(v -> finish());
        titleView = (TextView) findViewById(R.id.title_text);
        if (!isTemplate) { // 不使用模板就把标题栏去掉
            titleBar.setVisibility(View.GONE);
        }
        if (!isShowBackBtn) {
            templateButtonLeft.setVisibility(View.GONE);
        }
        mContext = this;
    }


    @Override
    public void setContentView(int layoutResID) {
        if (layoutResID == R.layout.template) {
            super.setContentView(layoutResID);
        } else {
            mainBody.removeAllViews();
            mainBody.addView(this.getLayoutInflater().inflate(layoutResID, null));
        }
        backLin = (LinearLayout)findViewById(R.id.lin_back);
        if(backLin != null){
            backLin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    @Override
    public void setContentView(View view) {
        mainBody.removeAllViews();
        mainBody.addView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mainBody.removeAllViews();
        mainBody.addView(view, params);
    }
    /**
     * 打印v级别信息
     *
     * @param msg
     */
    protected void log_v(String msg) {
        LoggerUtil.v(TAG, msg);
    }

    /**
     * 打印d级别信息
     *
     * @param msg
     */
    protected void log_d(String msg) {
        LoggerUtil.d(TAG, msg);
    }

    /**
     * 打印i级别信息
     *
     * @param msg
     */
    protected void log_i(String msg) {
        LoggerUtil.i(TAG, msg);
    }

    /**
     * 打印w级别信息
     *
     * @param msg
     */
    protected void log_w(String msg) {LoggerUtil.w(TAG, msg);}

    /**
     * 打印e级别信息
     *
     * @param msg
     */
    protected void log_e(String msg) {
        LoggerUtil.e(TAG, msg);
    }

    /**
     * 打印
     *
     * @param msg
     */
    protected void println(Object msg) {
        LoggerUtil.println(msg);
    }
}
